/**
 * Created by zhangyi on 2017/11/8.
 */
mainApp.factory("departFactory",function(){
    var service={};

    //获取所有列表service
    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list="";
        RequestUtils.postJson('../depart/get',obj).done(function(resp){
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });
        return list;
    };

    //新增部门的service
    service.addDepartData=function(departObj){
        RequestUtils.postForm("../depart/add",departObj)
            .done(function(resp){
                console.log(resp);
                if(resp.resultCode==0){
                    CommonUtils.buildFailHint("新增数据","成功");
                    location.reload(true);
                }else{
                    CommonUtils.buildFailHint("新增数据","失败");
                }
            });
    };

    //据id查询部门service
    service.getDepartById=function(id){
        var list="";
        RequestUtils.getJson("../depart/getById/"+id)
            .done(function(resp){
                console.log(resp);
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("查询数据","成功");
                }else{
                    CommonUtils.buildFailHint("查询数据","失败");
                }
            });
        return list;
    };

    //修改数据service
    service.update=function(departObj){
        RequestUtils.postForm("../depart/update",departObj)
            .done(function(resp){
                if(resp.resultCode==0){
                    CommonUtils.buildFailHint("修改数据","成功");
                    location.reload(true);
                }else{
                    CommonUtils.buildFailHint("修改数据","失败");
                }
            });
    };

    //删除数据serviec
    service.delete=function(id){
        RequestUtils.getJson("../depart/delete/"+id)
            .done(function(resp){
                if(resp.resultCode==0){
                    CommonUtils.buildFailHint("删除数据","成功");
                    location.reload(true);
                }else{
                    CommonUtils.buildFailHint("删除数据","失败");
                }
            });
    };

    //查询所有service
    service.selectAll=function(){
        var list="";
        RequestUtils.getJson('../depart/getAll').done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("查询数据","成功");
            }else{
                CommonUtils.buildFailHint('查询数据', '失败');
            }
        });
        return list;
    };

    return service;
});

mainApp.controller("departController",function($scope,$http,departFactory,empFactory){
    //首页
    $scope.listData=departFactory.getListData(1);
    $scope.list=$scope.listData.items;

    //打开新增模态框
    $scope.openAddDepartModal=function(){
        $("#addDepartModal").modal("show");
    };

    //新增
    $scope.addDepartData;
    $scope.departManager;
    $scope.addDepart=function(departName,managerObj){
        console.log(managerObj);
        var depart={
            "name":departName,
            "manager_name":managerObj.name,
            "phone":managerObj.phone,
            "email":managerObj.email,
            "manager":managerObj.id
        };
        console.log(depart);
        departFactory.addDepartData(depart);

        $("#addDepartModal").modal("hide");
        //$scope.$apply();
    };

    //在员工处查询的所有员工信息
    var empList=empFactory.selectAll();
    $scope.departManager=empList;

    /*//模拟下拉框选择员工名字，获取他的信息填写到部门表内
     $scope.departManager=[
     {id:1,name:"刘承军",phone:"2163534656",email:"2456243@qq.com"},
     {id:2,name:"张伊",phone:"217562394650",email:"2476879843@qq.com"},
     {id:4,name:"张三",phone:"217562394650",email:"2476879843@qq.com"},
     {id:3,name:"颜冠东",phone:"2132772852394650",email:"2476879986969@qq.com"}
     ]*/

    //打开修改模态框
    $scope.updateDepart;
    $scope.openUpdateDepartModal=function(id){
        var departObj=departFactory.getDepartById(id);
        //联查的个人信息
        $scope.updateManager={
            "id":departObj.manager,
            "name":departObj.manager_name,
            "phone":departObj.phone,
            "email":departObj.email
        }
        //一条部门信息
        $scope.updateDepart={
            "id":departObj.id,
            "name":departObj.name,
            "manager":departObj.manager,
            "manager_name":departObj.manager_name,
            "phone":departObj.phone,
            "email":departObj.email
        };

        $("#UpdateDepartModal").modal("show");
    };

    //修改数据
    $scope.Update=function(managerObj,departObj){
        var depart={
            "id":departObj.id,
            "name":departObj.name,
            "manager_name":managerObj.name,
            "phone":managerObj.phone,
            "email":managerObj.email,
            "manager":managerObj.id
        };
        console.log(depart);
        departFactory.update(depart);

        $("#UpdateDepartModal").modal("hide");
    };

    //删除数据
    $scope.delete=function(id){
        departFactory.delete(id);
    };


    //翻页
    $scope.NextListData=function(pageIndex){
        $scope.listData=departFactory.getListData(pageIndex);
        $scope.list=$scope.listData.items;
    }
});